<template>
  <div class="student-entry">
    <el-card class="entry-card">
      <template #header>
        <div class="card-header">
          <span>学生信息录入</span>
        </div>
      </template>
      
      <el-form 
        :model="studentForm" 
        :rules="rules" 
        ref="formRef" 
        label-width="120px"
        class="student-form"
      >
        <el-form-item label="姓名" prop="name">
          <el-input 
            v-model="studentForm.name" 
            placeholder="请输入学生姓名"
            clearable
          />
        </el-form-item>
        
        <el-form-item label="出生日期" prop="date">
          <el-date-picker 
            v-model="studentForm.date" 
            type="date" 
            placeholder="请选择出生日期"
            value-format="YYYY-MM-DD"
            style="width: 100%"
          />
        </el-form-item>
        
        <el-form-item label="地址" prop="address">
          <el-input 
            v-model="studentForm.address" 
            placeholder="请输入家庭地址"
            clearable
          />
        </el-form-item>
        
        <el-form-item label="联系电话" prop="phone">
          <el-input 
            v-model="studentForm.phone" 
            placeholder="请输入联系电话"
            clearable
          />
        </el-form-item>
        
        <el-form-item label="电子邮箱" prop="email">
          <el-input 
            v-model="studentForm.email" 
            placeholder="请输入电子邮箱"
            clearable
          />
        </el-form-item>
        
        <el-form-item class="form-buttons">
          <el-button type="primary" @click="onSubmit" :loading="loading">
            {{ loading ? '提交中...' : '提交' }}
          </el-button>
          <el-button @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
      
      <!-- 结果展示区域 -->
      <el-card class="result-card" v-if="studentStore.students.length > 0">
        <template #header>
          <div class="card-header">已录入学生信息</div>
        </template>
        <div class="result-content">
          <div v-for="(student, index) in studentStore.students" :key="index" class="student-result">
            <p>姓名：{{ student.name }}</p>
            <p>出生日期：{{ student.date }}</p>
            <p>地址：{{ student.address }}</p>
            <hr v-if="index < studentStore.students.length - 1">
          </div>
        </div>
      </el-card>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import { ElMessage } from 'element-plus'
import { useStudentStore } from '@/stores/student'

const studentStore = useStudentStore()
const formRef = ref<FormInstance>()
const loading = ref(false)

const studentForm = reactive({
  name: '',
  date: '',
  address: '',
  phone: '',
  email: ''
})

const rules = reactive<FormRules>({
  name: [
    { required: true, message: '请输入学生姓名', trigger: 'blur' },
    { min: 2, max: 10, message: '姓名长度应在2到10个字符之间', trigger: 'blur' }
  ],
  date: [
    { required: true, message: '请选择出生日期', trigger: 'change' }
  ],
  address: [
    { required: true, message: '请输入家庭地址', trigger: 'blur' }
  ],
  phone: [
    { required: false, message: '请输入联系电话', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
  ],
  email: [
    { required: false, message: '请输入电子邮箱', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
})

const onSubmit = async () => {
  if (!formRef.value) return
  
  await formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true
      
      // 模拟提交延迟
      setTimeout(() => {
        studentStore.addStudent({
          name: studentForm.name,
          date: studentForm.date,
          address: studentForm.address
        })
        
        ElMessage.success('学生信息录入成功')
        loading.value = false
        onReset()
      }, 500)
    } else {
      ElMessage.error('请检查表单填写是否正确')
    }
  })
}

const onReset = () => {
  studentForm.name = ''
  studentForm.date = ''
  studentForm.address = ''
  studentForm.phone = ''
  studentForm.email = ''
  
  if (formRef.value) {
    formRef.value.resetFields()
  }
}
</script>

<style scoped>
.student-entry {
  padding: 20px;
  height: 100%;
}

.entry-card {
  max-width: 800px;
  margin: 0 auto;
}

.card-header {
  font-size: 18px;
  font-weight: bold;
}

.student-form {
  max-width: 600px;
  margin: 0 auto;
}

.form-buttons {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 30px;
}

.result-card {
  margin-top: 30px;
}

.result-content {
  background-color: #f5f5f5;
  padding: 20px;
  border-radius: 4px;
  margin-top: 20px;
}

.el-form-item {
  margin-bottom: 22px;
}
</style>